const
  NMAX = 20;

type
  Interval = record
    t1, t2: integer;
  end;

var
  k, n, i, maxWaitTime, cameTime, curCameTime: integer;
  ints: array[0..NMAX] of Interval;

procedure readTwoDigitsNumber(var x: integer);
var
  c: char;
begin
  read(c);
  x := ord(c) - ord('0');
  read(c);
  x := x * 10 + ord(c) - ord('0');
end;

procedure ReadInterval(var int: Interval);
var
  skipChar: char;
  h1, m1, h2, m2: integer;
begin
  readTwoDigitsNumber(h1); read(skipChar);
  readTwoDigitsNumber(m1); read(skipChar);
  readTwoDigitsNumber(h2); read(skipChar);
  readTwoDigitsNumber(m2); readln;
  int.t1 := h1 * 60 + m1;
  int.t2 := h2 * 60 + m2;
end;

procedure WriteTime(t: integer);
var
  h, m: integer;
begin
  h := t div 60;
  m := t mod 60;
  if h < 10 then write('0');
  write(h,':');
  if m < 10 then write('0');
  write(m);
end;

begin
  readln(n);
  for i := 1 to n do
    ReadInterval(ints[i]);
  readln(k);
  with ints[0] do begin
    t1 := 8 * 60;
    t2 := 8 * 60;
  end;

  maxWaitTime := -1;
  cameTime := -1;
  curCameTime := 8 * 60;
  for i := 1 to n do begin
    if ints[i].t2 - ints[i].t1 >= k then begin
      if ints[i].t1 + k - curCameTime > maxWaitTime then begin
        maxWaitTime := ints[i].t1 + k - curCameTime;
        cameTime := curCameTime;
      end;
      curCameTime := ints[i].t2 - k + 1;
    end;
  end;

  WriteTime(cameTime);
  write(' ');
  WriteTime(cameTime + maxWaitTime);
  writeln;
end.
